package com.breakfast.fun.ui.user.coupon;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.user.CouponAdapter;
import com.breakfast.fun.adapter.user.CouponUselessAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.event.FinishEvent;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.ui.web.WebActivity;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.TitleBarView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yukai on 2016/11/9.
 */

public class CouponUselessActivity extends BaseActivity<CouponPresenter, CouponModel> implements CouponContract.View {


    @Bind(R.id.tbv_user_coupon_useless)
    TitleBarView mTbvUserCoupon;
    @Bind(R.id.ptrlv_coupon_useless)
    PullToRefreshListView mPtrlvCoupon;
    @Bind(R.id.iv_user_coupon_empty_useless)
    ImageView mIvUserCouponEmpty;
    @Bind(R.id.tv_user_coupon_info_useless)
    TextView mTvUserCouponInfo;

    private CouponUselessAdapter adapter;

    private int begin = 2;


    @Override
    public int getLayoutId() {
        return R.layout.activity_user_coupon_useless;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mTbvUserCoupon.setTitleNoRight("已过期优惠券");
        mTbvUserCoupon.setBack(this);


        mPtrlvCoupon.setMode(PullToRefreshBase.Mode.BOTH);

        mPtrlvCoupon.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                begin = 0;
                presenter.getCouponTimeOut(begin, false);
                begin++;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                presenter.getCouponTimeOut(begin, true);
            }

        });


        mTvUserCouponInfo.setOnClickListener(v -> {
            Intent intent = new Intent(CouponUselessActivity.this, WebActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("title", "优惠券使用说明");
            intent.putExtra("des", "抽奖优惠券说明");
            intent.putExtra("url", "http://www.52letsgo.cn/fe/user/fqa/q_coupon.html");
            startActivity(intent);
        });
    }

    @Override
    public void showCoupon(Coupon coupon) {

        List<Coupon.CouponData> list = new ArrayList<Coupon.CouponData>();
        for (Coupon.CouponData data : coupon.getData()) {
            if (data.getIsTimeout() != 0) {
                list.add(data);
            }
        }
        if (coupon == null || list == null || list.size() == 0) {
            mIvUserCouponEmpty.setVisibility(View.VISIBLE);
            T.show("暂无记录");
            return;
        } else {
            mIvUserCouponEmpty.setVisibility(View.GONE);
            begin = 10;
        }
        adapter = new CouponUselessAdapter(context, list);
        mPtrlvCoupon.setAdapter(adapter);
    }


    @Override
    public void loadCoupon(Coupon coupon) {
        if (coupon.getData().size() == 0) {
            T.show("已无更多数据");
        } else {
            begin++;
            adapter.addALL(coupon.getData());
            adapter.notifyDataSetChanged();
        }
    }

//    @Override
//    public void toPay(PayBean payBean) {
//        Intent intent = new Intent(getActivity(), BFPayActivity.class);
//        intent.putExtra("payBean",payBean);
//        startActivity(intent);
//    }

    @Override
    public void stopRefresh() {
        mPtrlvCoupon.onRefreshComplete();
    }

//    @Subscribe
//    public void onEventPay(PayEvent event){
//        if(event.getClassName().equals(this.getClass().getSimpleName())) {
//            presenter.getPayBean(event.getOrderNo());
//        }
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventRefresh(RefreshEvent event) {
        if (event.getActivityName().equals(getClass().getSimpleName())) {
            begin = 0;
            presenter.getCouponTimeOut(begin, false);
            begin++;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventFinish(FinishEvent event) {
        if (event.getActivityName().equals(getClass().getSimpleName())) {
            finish();
        }
    }


//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        EventBus.getDefault().unregister(this);
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
